import { login, getUserInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
const state = {
  token: getToken() || null,
  UserInfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  getUserInfo(state, Info) {
    state.UserInfo = Info
  },
  removeToken(state) {
    state.token = null
    removeToken()
  },
  removeUserInfo(state) {
    state.UserInfo = {}
  }
}
const actions = {
  async login(context, data) {
    const { data: { token }} = await login(data) // 拿到token
    console.log(888, token)
    context.commit('setToken', token) // 设置token
  },
  async getUserInfo({ commit }) {
    const data = await getUserInfo()
    console.log(data)
    commit('getUserInfo', data)
  },
  loginout(context) {
    context.commit('removeUserInfo')
    context.commit('removeToken')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

